﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DVSProject.Models;

namespace DVSProject.Lib
{
    public class TitleMwwDispenserRepo
    {
        public const string m_msg_head = "Title MwwDispenser";

        public IEnumerable<TitleMwwDispenserModels> GetTitleMwwDispenser()
        {
            List<TitleMwwDispenserModels> TitleMwwDispensers = new List<TitleMwwDispenserModels>();
            string SQLCommand = @"select * from TBTitleMwwDispenser WHERE Status <> 'Delete' Order by TitleMwwDispenserId";
            using (var db = new DBHelper())
            {
                try
                {
                    var reader = db.ExecuteReader(SQLCommand);
                    while (reader.Read())
                    {
                        TitleMwwDispenserModels TitleMwwDispense = new TitleMwwDispenserModels();
                        TitleMwwDispense.TitleMwwDispenserId = (int)reader["TitleMwwDispenserId"];
                        TitleMwwDispense.TitleMwwDispenserName = (string)reader["TitleMwwDispenserName"];
                        TitleMwwDispense.Status = (string)reader["Status"];

                        TitleMwwDispensers.Add(TitleMwwDispense);
                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0001", ex));
                }
            }
            return TitleMwwDispensers.ToList();
        }

        public TitleMwwDispenserModels GetTitleMwwDispenserById(int id)
        {
            TitleMwwDispenserModels TitleMwwDispenser = new TitleMwwDispenserModels();
            string SQLCommand = @"select * from TBTitleMwwDispenser where TitleMwwDispenserId=@TitleMwwDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.AddParameter("@TitleMwwDispenserId", id);
                    var reader = db.ExecuteReader(SQLCommand);
                    if (reader.Read())
                    {
                        TitleMwwDispenser.TitleMwwDispenserId = (int)reader["TitleMwwDispenserId"];
                        TitleMwwDispenser.TitleMwwDispenserName = (string)reader["TitleMwwDispenserName"];
                        TitleMwwDispenser.Status = (string)reader["Status"];

                    }
                }
                catch (Exception ex)
                {
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0002", ex));
                }
            }
            return TitleMwwDispenser;
        }

        public bool Create(TitleMwwDispenserModels TitleMwwDispenser)
        {
            bool result = true;
            string SQLCommand = @"INSERT into TBTitleMwwDispenser
                                    values (@TitleMwwDispenser,@Status)";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMwwDispenserName", TitleMwwDispenser.TitleMwwDispenserName);
                    db.AddParameter("@Status", "Active");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0003", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Edit(TitleMwwDispenserModels TitleMwwDispenser)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleMwwDispenser 
                                    Set TitleMwwDispenserName=@TitleMwwDispenserName
                                    where TitleMwwDispenserId=@TitleMwwDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMwwDispenserId", TitleMwwDispenser.TitleMwwDispenserId);
                    db.AddParameter("@TitleMwwDispenserName", TitleMwwDispenser.TitleMwwDispenserName);

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0004", ex));
                    return false;
                }
            }
            return result;
        }

        public bool Delete(TitleMwwDispenserModels TitleMwwDispenser)
        {
            bool result = true;
            string SQLCommand = @"Update TBTitleMwwDispenser
                                    Set Status=@Status
                                    where TitleMwwDispenserId=@TitleMwwDispenserId";

            using (var db = new DBHelper())
            {
                try
                {
                    db.BeginTransaction();
                    db.AddParameter("@TitleMwwDispenserId", TitleMwwDispenser.TitleMwwDispenserId);
                    db.AddParameter("@Status", "Delete");

                    db.ExecuteNonQuery(SQLCommand, DbConnectionState.KeepOpen);
                    db.CommitTransaction();
                }
                catch (Exception ex)
                {
                    db.RollbackTransaction();
                    TraceLog.WriteError(string.Format("{0}_{1}->{2}", m_msg_head, "0005", ex));
                    return false;
                }
            }
            return result;
        }
    }
}